package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JOptionPane;

import db.parking.beans.Complaints;


import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.forms.ComplaintForm;
import vcp.gui.forms.validators.NumericValidator;

import vcp.models.ComplaintsModel;
import vcp.views.ComplaintsView;

/**
 * Complaints controller
 * @author G15
 *
 */
public class ComplaintController extends AbstractController implements
ActionListener, IController {

	ComplaintsView view;
	ComplaintsModel model;
	
	@Override
	public void addModel(Object model) {
		this.model = (ComplaintsModel)model;		
	}

	@Override
	public void addView(Object view) {
		this.view = (ComplaintsView) view;
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		addComplaint();
	}
	
	private void addComplaint() {
		Complaints bean = new Complaints();
		NumericValidator verifyID = new NumericValidator();
		ComplaintForm form = view.getComplaintsForm();
	
		if(form.getTextOOID().getText().isEmpty() ||form.getTextOTitle().getText().isEmpty()||form.getTextOOID().getText().isEmpty() ){//Check if all fields are fills
        	JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		    return;
		}
		
		if(!verifyID.validate(form.getTextOOID().getText()))
		{
        	JOptionPane.showMessageDialog(null,"Order Id can onley be a number","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
		    return;
		}

		
		bean.setAmount(0);
		bean.setOrderID(Integer.parseInt(form.getTextOOID().getText()));
		bean.setTitle(form.getTextOTitle().getText());
		bean.setDescription(form.getTextAreaODescr().getText());
		
		bean.setCustomerID("0");
		bean.setStatus(0);
		
		try{
			model.submitComplaint(bean);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
	
		}		
	}
}


